import Vue from 'vue';
import Router from "@/router/";
import Store from "@/store";
import Preferences from "../config/preferences";
import el from 'element-ui';


const $el = {
  $loading: el.Loading.service,
  $msgbox: el.MessageBox,
  $alert: el.MessageBox.alert,
  $confirm: el.MessageBox.confirm,
  $prompt: el.MessageBox.prompt,
  $notify: el.Notification,
  $message: el.Message,
}

  // 路由跳转
export  default {
  back: function(){
    Router.back();
  },
  to(name, query){
    var option = {
      name: name,
    };
    if (query) {
      option.query = query;
    }
    Router.push(option);
  },
  toError(code){
    Router.push({path:"/error",query:{errorCode: code}})
  },
  replace(name, query){
    var option = {
      name: name,
    };
    if (query) {
      option.query = query;
    }
    this.close()
    setTimeout(()=>{
      Router.push(option);
    },0)
  },
  close(view,back){
    view = view || Router.currentRoute
    function toLastView(visitedViews, view) {
      const latestView = visitedViews.slice(-1)[0]
      if (latestView) {
        Router.push(latestView.fullPath)
      } else {
        // now the default is to redirect to the home page if there is no tags-view,
        // you can adjust it according to your needs.
        if (view.name === 'Dashboard') {
          // to reload home page
          Router.replace({ path: '/redirect' + view.fullPath })
        } else {
          Router.push('/')
        }
      }
    }
    Store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
      // toLastView(visitedViews, view)
      this.back()
    })
  },
  redirect(path){
    console.log('ptah', path)
    Router.replace({
      path: '/redirect' + path
    }).catch((err)=>{})
  },

  message(message, type, options){
    $el.$message({
      showClose: true,
      message: message || '',
      type: type,
      ...options
    });
  },
  confirm(message,title,options){
    title = title || '提示'
    options = {
      type: 'warning',
      ...options
    }
    return $el.$confirm(message,title, options)
  },
  setTitle(title, $route){
    $route.meta.title = title;
  },
  setSubtitle(title, $route){
    $route.meta.subtitle = title;
  },
  setMeta(that, key, value){
    that.$set(
      that.$route.meta,
      key,
      value
    );
    // $route.meta.subtitle = subtitle
  },
  copyText(text){
    let url = text;
    let oInput = document.createElement('input');
    oInput.value = url;
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象;
    // console.log(oInput.value)
    document.execCommand("Copy"); // 执行浏览器复制命令
    this.message('已成功复制到剪切板', 'success');
    oInput.remove()
  },
  getTableHeight(line, lineheight) {
    line =
      line ||
      Preferences.TablePageSizes[Preferences.TablePageSizes.length - 1];
    lineheight = lineheight || 65;
    var result = 48 + lineheight * line;
    return result;
  },
  // 下载 简单下载 仅需要url就可以下载的简单方式
  downloadSimple: function (url, name) {
    name = name || "download";
    if (url) {
      var a = document.createElement("a"); // 创建一个a节点插入的document
      var event = new MouseEvent("click"); // 模拟鼠标click点击事件
      a.download = name; // 设置a节点的download属性值
      a.href = url; // 将图片的src赋值给a节点的href
      a.target = "_blank";
      a.dispatchEvent(event); // 触发鼠标点击事件
    }
  },
  // 下载 下载图片
  downloadImage(url, name) {
    name = name || "download";
    let src = url;
    var canvas = document.createElement("canvas");
    var img = document.createElement("img");
    img.onload = function () {
      canvas.width = img.width;
      canvas.height = img.height;
      var context = canvas.getContext("2d");
      context.drawImage(img, 0, 0, img.width, img.height);
      canvas.getContext("2d").drawImage(img, 0, 0, img.width, img.height);
      canvas.toBlob((blob) => {
        let link = document.createElement("a");
        link.href = window.URL.createObjectURL(blob);
        link.download = name;
        link.click();
      }, "image/jpeg");
    };
    img.setAttribute("crossOrigin", "Anonymous");
    img.src = src;
  },
  // 获取页码 （最大最小页面限制）
  getDeletedPage(page, pageSize, total){
    let toatalPage = Math.ceil((total-1)/pageSize)
    if(page > toatalPage){
      page = toatalPage
    }
    if(page < 1){
      page = 1
    }
    return page
  },
}
